package com.yf.ability.config.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.ability.config.dto.CfgBannerDTO;
import com.yf.ability.config.entity.CfgBanner;
import com.yf.ability.config.mapper.CfgBannerMapper;
import com.yf.ability.config.service.CfgBannerService;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.base.utils.BeanMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* <p>
* 轮播图业务实现类
* </p>
*
* @author 聪明笨狗
* @since 2022-11-18 10:37
*/
@Service
public class CfgBannerServiceImpl extends ServiceImpl<CfgBannerMapper, CfgBanner> implements CfgBannerService {

    @Override
    public IPage<CfgBannerDTO> paging(PagingReqDTO<CfgBannerDTO> reqDTO) {

        //查询条件
        QueryWrapper<CfgBanner> wrapper = new QueryWrapper<>();

        // 请求参数
        CfgBannerDTO params = reqDTO.getParams();

        if(params!=null){
            if(!StringUtils.isEmpty(params.getPlatform())){
                wrapper.lambda().eq(CfgBanner::getPlatform, params.getPlatform());
            }
        }

        wrapper.lambda().orderByDesc(CfgBanner::getSort);

        //获得数据
        IPage<CfgBanner> page = this.page(reqDTO.toPage(), wrapper);
        //转换结果
        IPage<CfgBannerDTO> pageData = JSON.parseObject(JSON.toJSONString(page), new TypeReference<Page<CfgBannerDTO>>(){});
        return pageData;
    }


    @Override
    public void save(CfgBannerDTO reqDTO){
        //复制参数
        CfgBanner entity = new CfgBanner();
        BeanMapper.copy(reqDTO, entity);
        this.saveOrUpdate(entity);
    }

    @Override
    public void delete(List<String> ids){
        //批量删除
        this.removeByIds(ids);
    }

    @Override
    public CfgBannerDTO detail(String id){
        CfgBanner entity = this.getById(id);
        CfgBannerDTO dto = new CfgBannerDTO();
        BeanMapper.copy(entity, dto);
        return dto;
    }
}
